package com.cskaoyan.service;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.bean.vo.promotion_vo.TopicListVO;
import com.cskaoyan.bean.vo.promotion_vo.TopicReadVO;
import com.cskaoyan.mapper.WxTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxTopicServiceImpl implements WxTopicService{
    @Autowired
    WxTopicMapper wxTopicMapper;

    @Override
    public TopicListVO list(BaseParam baseParam) {
        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
//        String sort = baseParam.getSort();
//        String order = baseParam.getOrder();
        PageHelper.startPage(page, limit);
        List<Topic> topics = wxTopicMapper.list();
        PageInfo<Topic> pageInfo = new PageInfo(topics);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        TopicListVO listVO = new TopicListVO();
        listVO.setTotal(total);
        listVO.setPages(pages);
        listVO.setLimit(limit);
        listVO.setPage(page);
        listVO.setList(topics);

        return listVO;
    }

    @Override
    public TopicReadVO detail(Integer id) {
        Topic topic = wxTopicMapper.selectById(id);
        Integer[] goods = topic.getGoods();
        List<Goods> goodsList = wxTopicMapper.listGoods(goods);

        TopicReadVO topicReadVO = new TopicReadVO();
        topicReadVO.setTopic(topic);
        topicReadVO.setGoodsList(goodsList);

        return topicReadVO;
    }

    @Override
    public List<Topic> related(Integer id) {
        List<Topic> topics = wxTopicMapper.listRelated(id);
        return topics;
    }
}
